<?php session_start(); ?>
<?php
include('../ketnoi.php');
if (!$_SESSION['user']) header("location: index.php"); // nếu biến session['banguyen'] chưa có thì trở về trang đăng nhập, copy dòng này vào trang nào muốn bảo vệ bằng mật khẩu
?>
<style type="text/css">
<!--
.error, .notice, .success {padding:.8em;margin:auto; margin-bottom:.5em;border:2px solid #AAF;width:570px;text-align: center}
.error {background: #ECECFF;color: red;border-color:#AAF;}
-->
</style>
<script type="text/javascript" src="scripts/joomla.js"></script>
<div class="m">
 <?php 
    $shop=$dulieu;
	$trangthai="";
	if($_GET['action']=='view' && $_GET['option']=='com_user')
	{
		$sql = "SELECT * FROM `".$shop."`.`nhanvien` order by NV_MA asc";    	
		$ketqua = mysql_query($sql) or die ('khong tim thay du lieu');
		$tong=mysql_num_rows($ketqua);
		$_SESSION['limit']=$_POST['limit'];
		if($_SESSION['limit']==""){$sotrang = ceil($tong/20);}else{
		$sotrang = ceil($tong/$_SESSION['limit']);}
	    $dem = -1;
		
	if(isset($_POST['trang'])){
	 $trang = $_POST['trang'];
	}else{	
     if(isset($_POST['end'])){
	  $trang =$sotrang;
	 }else{
	  if(isset($_POST['start'])){
	   $trang =1;
	  }else{
	   $trang=1;
	  }
	 }	
	}
?>
<div id="menu">
	<div style="float: right;">
	<div class="icon1">
<a  class="toolbar" href="index.php"><img src="images/toolbar/icon-32-cancel.png" alt="Cencel"><br><span>Cancel</span></a>
	</div>
	</div>

	
	<div style="float: right;">
	<div class="icon1">
<a class="toolbar" href="?option=com_user&action=insert"><img src="images/toolbar/icon-32-new.png" alt="New"><br><span>New</span></a>
	</div>
	</div>
</div>
<div class="header" style="background:url(images/dashboard/shop_users.png) no-repeat scroll 0% 0% transparent; text-indent: 30px; line-height: 50px;">
  <h2 style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;Danh sách nhân viên </h2>
</div><br /><br />
<form name="adminForm" action="" method="post">
					
					<input name="option" value="com_user" type="hidden">
					<input name="boxchecked" type="hidden">
					<input name="task" value="" type="hidden">
					<input name="func" value="" type="hidden">

<table width="100%"><tr>
<td width="30%">Lọc 
					  <select name="trangthai" id="filter_state" class="inputbox" size="1" onchange="adminForm.submit();">
					  <option value="" selected="selected">- Chọn trạng thái -</option>
					  <option value="0">Đã được bật</option>
					  <option value="1">Đã tắt</option>
					  </select>		</td>
<td width="70%">
				<input class="inputbox" size="25" name="keyword" type="text">
				<input class="button_1" name="search" value="Tìm kiếm" type="submit">
</td></tr>
<tr>
<td colspan="2">&nbsp;
</td>
</tr>
</table>
<?php
		 $keyword = $_POST['keyword'];
		 $trangthai = $_POST['trangthai'];
 if($keyword != ""){
         $query = "select * from `".$shop."`.`nhanvien` where NV_MA = '".$keyword."' or NV_TEN like '%".$keyword."%'";
		 $ketqua = mysql_query($query);
		 $tong1=mysql_num_rows($ketqua);
 }
 if($trangthai != ""){
         $query = "select * from `".$shop."`.`nhanvien` where NV_HTRANG = '".$trangthai."%'";
		 $ketqua = mysql_query($query);
		 $tong1=mysql_num_rows($ketqua); 	
 }
?>

<table class="adminlist" width="100%">
<tbody><tr><th width="3%" class="title">#</th>
<th width="3%" class="title"><input name="toggle" value="" onclick="checkAll(<?php if($_SESSION['limit']==""){echo '20';}else{echo $_SESSION['limit']; } ?>)" type="checkbox"></th>
<th class="title" width="10%"> Tên đăng nhập</th>
<th class="title" width="13%"> Tên nhân viên</th>
<th class="title" width="14%"> Email</th>
<th class="title" width="9%"> Được bật</th>
<th class="title" width="22%"> Địa Chỉ </th>
<th class="title" width="10%"> Số ĐT</th>
<th class="title" width="8%"> Ngày làm việc</th>
<th width="4%" class="title"> Sửa</th>
<th class="title" width="4%"> Xóa</th>
</tr>
 <?php 
	$stt=0;
	$t=0;
   while($dongDL = mysql_fetch_array($ketqua))
	  { 
	  $dem++;
	    if($_SESSION['limit']==""){$s=20;}
		else{$s=$_SESSION['limit'];}
		if($dem >= ($trang-1)*$s && $dem <= ($trang*$s)-1)
		{
  ?>
  
<tr class="row<?php echo $t ?>">
<td height="22"><?php echo $dem+1 ?></td>
<td><input id="cb<?php echo $stt ?>" name="product_id[<?php echo $dongDL['NV_MA']?>]" value="<?php echo $dongDL['NV_MA']?>" onclick="isChecked(this.checked);" type="checkbox"></td>
<td><?php echo $dongDL['NV_MA']?></td>
<td><?php echo $dongDL['NV_TEN']?></td>
<td><?php echo $dongDL['NV_EMAIL']?></td>
<td align="center"><a href="?option=com_user&action=update_tt&amp;id=<?php echo $dongDL['NV_MA'] ?>"><?php if($dongDL['NV_HTRANG']==1){echo "<img src='images/publish_x.png'>"; }else{ echo "<img src='images/tick.png'>";}?></a></td>
<td><?php echo $dongDL['NV_DIACHI']?></td>
<td><?php echo $dongDL['NV_SDT']?> </td>  	 
<td><?php 
    echo substr($dongDL['NV_NGAYDK'],8,2)."-".substr($dongDL['NV_NGAYDK'],5,2)."-".substr($dongDL['NV_NGAYDK'],0,4).' &nbsp;'; 
?></td>
<td><a href="?option=com_user&action=update&amp;id=<?php echo $dongDL['NV_MA'] ?>"><img src="images/edit_f2.png" width="20" height="20" name="Edit<?php echo $stt+1 ?>" alt="Edit" border="0"></a></td>
<td><a class="toolbar" href="?option=com_user&action=delete&amp;id=<?php echo $dongDL['NV_MA'] ?>" onclick="return confirm('Bạn có chắc chắn muốn xóa?');"><img src="images/delete_f2.png" width="20" height="20"alt="Delete" name="delete<?php echo $stt+1 ?>" align="middle" border="0"></a></td>
</tr>
	 <?php
	 $stt=$stt+1;
	 if($t==0)$t=1;
	 else $t=0;
	 }
	  }//ket thuc ham while in du lieu
	  ?>
</tbody></table>

<table class="adminlist"><tbody>
<?php if($sotrang > 1){?>
<tr>
  <td colspan="3" align="center"> 
  <input name="start" class="button2" type="submit" value="Start"/>
  <?php
for($i=1; $i<=$sotrang; $i++)
{ ?>
	<input name="trang" class="button" type="submit" value="<?php echo $i;?>"/>
<?php 
}
?>
<input name="end" class="button2" type="submit" value="End"/></td></tr>
<?php } ?>
<?php 
if($keyword=="" && $trangthai==""){
?>
<tr><td width="48%" align="right" nowrap="nowrap">Display #</td><td>
<select class="inputbox" name="limit" size="1" onchange="this.form.submit();">
<option value="5"  <?php if($_SESSION['limit']==5) echo "selected='selected'" ?>>5</option>
<option value="10" <?php if($_SESSION['limit']==10) echo "selected='selected'" ?>>10</option>
<option value="15" <?php if($_SESSION['limit']==15) echo "selected='selected'" ?>>15</option>
<option value="20" <?php if($_SESSION['limit']=='') echo "selected='selected'" ?>>20</option>
<option value="25" <?php if($_SESSION['limit']==25) echo "selected='selected'" ?>>25</option>
<option value="30" <?php if($_SESSION['limit']==30) echo "selected='selected'" ?>>30</option>
<option value="50" <?php if($_SESSION['limit']==35) echo "selected='selected'" ?>>50</option>
</select>
</td><td width="48%" align="left" nowrap="nowrap">Results 1 - 
<?php if($_SESSION['limit']==''){if($tong < 20 ){echo $tong;}else echo '20';} else{if($_SESSION['limit'] > $tong ){echo $tong;}else echo $_SESSION['limit'];}?> of <?php echo $tong ?></td></tr>
<?php }else{
?>
<tr><td width="48%" align="right" nowrap="nowrap">Display #</td><td>
<select class="inputbox" name="limit" size="1" disabled="disabled">
<option><?php echo $tong1; ?></option>
</select>
</td><td width="48%" align="left" nowrap="nowrap"><?php if($tong1 >0){?>Results 1 - 
<?php echo $tong1;?> of <?php echo $tong1; }?></td></tr>
<?php
}
?>
</tbody></table>
</form>
<?php
}//ket thuc view

if($_GET['action']=='delete' && $_GET['option']=='com_user')
	{
		  $sql = "Delete from `".$shop."`.`nhanvien` where NV_MA = '".$_GET['id']."'";
		  $delete = mysql_query($sql);
          echo"<script language=javascript>window.location='index.php?option=com_user&action=view';</script>";
	  }// ket thuc kiem tra action Delete
	  
if($_GET['action']=='insert' && $_GET['option']=='com_user')
{
	
if(isset($_POST['s1'])){ 
	$error="";
	if($_POST['s1']=="" || $_POST['s2']=="" || $_POST['s3']=="" || $_POST['s7']==""){
	  $error="Vui lòng nhập đầy đủ các field.";
    }else{ 
	  $kt = "select * from `".$dulieu."`.`nhanvien` where NV_MA='".$_POST['s2']."'";
	  $kq = mysql_query($kt);
	  if (mysql_num_rows($kq) > 0){
        $error = "Lỗi: Tên đăng nhập này đã tồn tại, vui lòng nhập tên khác";
      }else{
	     if ($_POST['s7'] != $_POST['s8']){
          $error = "Lỗi: password lập lại không đúng";
         }else{
           $sql = "INSERT INTO `".$shop."`.`nhanvien` (`NV_TEN`,`NV_MA`,`NV_DIACHI`,`NV_SDT`,`NV_PASS`,`NV_HTRANG`,`NV_NGAYDK`,`NV_EMAIL`) VALUES ('".$_POST['s1']."', '".$_POST['s2']."', '".$_POST['s3']."', '".$_POST['s4']."', '".md5(md5($_POST['s7']))."', '".$_POST['s9']."',now(),'".$_POST['s10']."');";
		   $insert = mysql_query($sql);
           echo"<script language=javascript>window.location='index.php?option=com_user&action=view';</script>";		  	 	 	 	  
		 }
	  }
	}
}


	?>
	<div id="menu">
	<div style="float: right;">
	<div class="icon1">
<a  class="toolbar" href="?option=com_user&action=view"><img src="images/toolbar/icon-32-cancel.png" alt="Remove"><br><span>Cancel</span></a>
	</div>
	</div>
	
	<div style="float: right;">
	<div class="icon1">
<a class="toolbar" onclick="adminForm2.submit();"><img src="images/toolbar/icon-32-save.png"  alt="New"><br><span>Save</span></a>
	</div>
	</div>
</div>

<div class="header" style="background: url(images/dashboard/shop_users.png) no-repeat scroll 0% 0% transparent; text-indent: 30px; line-height: 50px;">
  <h2 style="margin: 0px;">&nbsp;&nbsp;&nbsp; Nhân viên: [ Thêm mới ]</h2>
</div><br /><br />
<?php
	if($error!=""){
	?>
	 <div class="error" align="center"><img src='../images/cancel_f2.png' width='10px' height='10px'/> <?php echo $error ?></div>
  <?php
  }
?>
	<form name="adminForm2" action="" method="post" enctype="multipart/form-data">

<div style="height: auto; width: 1012px;" id="ext-gen37">
<div class="tab-page" id="info-page">
<table width="1000" class="adminform">
	<tbody><tr> 
   		<td width="461" valign="top">
			<table width="100%" border="0">
      			<tbody><tr> 
       				<td colspan="2" align="left"><h3>Chi tiết nhân viên</h3></td>
    			</tr>
    			<tr class="row1"> 
      				<td width="32%"><div style="text-align: right; font-weight: bold;">
   					  Tên:</div>      				</td>
      				<td width="68%"> 
       				  <input name="s1" size="32" style="width:200px; margin-left:10px" maxlength="255" type="text">      				</td>
    			</tr>
    			<tr class="row0"> 
      				<td width="32%"><div style="text-align: right; font-weight: bold;">
					Tên đăng nhập:</div>      				</td>
      				<td width="68%"><input style="width:200px; margin-left:10px" name="s2" size="32" maxlength="255" type="text" /></td>
    			</tr>
    			<tr class="row1"> 
      				<td width="32%"><div style="text-align: right; font-weight: bold;">Địa chỉ:</div>      				</td>
      				<td width="68%"><input style="width:200px; margin-left:10px" name="s3" size="32" maxlength="255" type="text" /></td>
    			</tr>
				<tr class="row0"> 
      				<td width="32%"><div style="text-align: right; font-weight: bold;">
					Số điện thoại:</div>      				</td>
      				<td width="68%"><input style="width:200px; margin-left:10px" name="s4" size="32" maxlength="255" type="text" /></td>
    			</tr>
                <tr class="row1"> 
      				<td width="32%"><div style="text-align: right; font-weight: bold;">
					Email:</div>      				</td>
      				<td width="68%"><input style="width:200px; margin-left:10px" name="s10" size="32" maxlength="255" type="text" /></td>
    			</tr>
				 <tr class="row0"> 
				   <td width="32%"><div style="text-align: right; font-weight: bold;">Mật khẩu mới:</div>      				</td>
				   <td width="68%"><input style="width:200px; margin-left:10px" name="s7" size="32" maxlength="255" type="text" /></td>
				   </tr>
				<tr class="row1"> 
      				<td width="32%"><div style="text-align: right; font-weight: bold;">Xác nhận mật khẩu:</div>      				</td>
      				<td width="68%"><input style="width:200px; margin-left:10px" name="s8" size="32" maxlength="255" type="text"> </td>
    			</tr>
				<tr class="row0"> 
      				<td width="32%"><div style="text-align: right; font-weight: bold;">Khóa nhân viên:</div>      				</td>
      				<td width="68%">
					 <input type="radio" name="s9" value="1"/> Có
                      <input type="radio" name="s9" value="0" checked="checked" /> Không 
					</td>
    			</tr>
    			  <tr class="row0"> 
    					    
		      		<td valign="top" width="32%">&nbsp;</td>
		      		<td width="68%">&nbsp;</td>		    
	    			</tr>
	  			  </tbody></table>
 		</td>
 		<td width="527">&nbsp;</td>
  	</tr>
</tbody></table>

</div>
</div>
</form>
<?php
}// ket thuc kiem tra action insert



if($_GET['action']=='update' && $_GET['option']=='com_user')
	{
    if($_GET['id'] !=""){
	    $_SESSION['ma']= $_GET['id'];
	}
		$sql ="SELECT * from `".$shop."`.`nhanvien` where NV_MA = '".$_SESSION['ma']."'";
		$ketqua = mysql_query($sql);
		$dulieu = mysql_fetch_array($ketqua);
		
if(isset($_POST['ts1'])){  
  $error="";
  if($_POST['ts1']=="" || $_POST['ts2']=="" || $_POST['ts3']=="" || $_POST['ts4']=="" || $_POST['ts10']==""){
	  $error="Vui lòng nhập đầy đủ các field.";
  }else{ 
	if ($_POST['ts7'] != $_POST['ts8']){
        $error = "password lập lại không đúng";
    }else{
	   if($_SESSION['ma'] != $_POST['ts2']){
         $kt = "select * from `".$dulieu."`.`nhanvien` where NV_MA='".$_POST['ts2']."'";
	     $kq = mysql_query($kt);
	     if (mysql_num_rows($kq) > 0){
             $error = "Tên đăng nhập này đã tồn tại, vui lòng chọn tên khác";
		 }else{
			if($_POST['ts7']==""){
	           $query = "update `".$shop."`.`nhanvien` set NV_EMAIL='".$_POST['ts10']."', NV_MA='".$_POST['ts2']."', NV_TEN='".$_POST['ts1']."', email_kh='".$_POST['ts6']."', NV_DIACHI='".$_POST['ts3']."', NV_SDT='".$_POST['ts4']."', NV_HTRANG='".$_POST['ts9']."' where NV_MA='".$_SESSION['ma']."'" ;
               mysql_query($query);  
	           mysql_close();
	        }else{
	           $query = "update `".$shop."`.`nhanvien` set NV_MA='".$_POST['ts2']."', NV_PASS='".md5(md5($_POST['ts7']))."',NV_TEN='".$_POST['ts1']."', NV_DIACHI='".$_POST['ts3']."', NV_SDT='".$_POST['ts4']."', NV_HTRANG='".$_POST['ts9']."' where NV_MA='".$_SESSION['ma']."'" ;
               mysql_query($query);  
	           mysql_close();
	        } 
	        echo"<script language=javascript>window.location='index.php?option=com_user&action=view';</script>";
		 }
	   }else{
            if($_POST['ts7']==""){
	           $query = "update `".$shop."`.`nhanvien` set NV_MA='".$_POST['ts2']."', NV_TEN='".$_POST['ts1']."', NV_DIACHI='".$_POST['ts3']."', NV_SDT='".$_POST['ts4']."', NV_HTRANG='".$_POST['ts9']."' where NV_MA='".$_SESSION['ma']."'" ;
               mysql_query($query);  
	           mysql_close();
	        }else{
	           $query = "update `".$shop."`.`nhanvien` set NV_MA='".$_POST['ts2']."', NV_PASS='".md5(md5($_POST['ts7']))."',NV_TEN='".$_POST['ts1']."', NV_DIACHI='".$_POST['ts3']."', NV_SDT='".$_POST['ts4']."', NV_HTRANG='".$_POST['ts9']."' where NV_MA='".$_SESSION['ma']."'" ;
               mysql_query($query);  
	           mysql_close();
	        } 
	        echo"<script language=javascript>window.location='index.php?option=com_user&action=view';</script>";
	   }
	}	   
  }    
}
	?>
<div id="menu">
	<div style="float: right;">
	<div class="icon1">
<a  class="toolbar" href="?option=com_user&action=view"><img src="images/toolbar/icon-32-cancel.png" alt="Cancel"><br><span>Cancel</span></a>
	</div>
	</div>
	
	<div style="float: right;">
	<div class="icon1">
<a class="toolbar" onclick="adminForm3.submit();"><img src="images/toolbar/icon-32-save.png"  alt="New"><br><span>Save</span></a>
	</div>
	</div>
</div>

<div class="header" style="background: url(images/dashboard/shop_users.png) no-repeat scroll 0% 0% transparent; text-indent: 30px; line-height: 50px;">
<h2 style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;Nhân viên: [ Sửa ]</h2></div><br /><br />
<?php
	if($error!=""){
	?>
	 <div class="error" align="center"><img src='../images/cancel_f2.png' width='10px' height='10px'/> <?php echo $error ?></div>
  <?php
  }
?>
		<form name="adminForm3" action="" method="post" enctype="multipart/form-data">

<div style="height: auto; width: 1012px;" id="ext-gen37">
<div class="tab-page" id="info-page">
<table width="1000" class="adminform">
	<tbody><tr> 
   		<td width="461" valign="top">
			<table width="100%" border="0">
      			<tbody><tr> 
       				<td colspan="2" align="left"><h3>Chi tiết nhân viên</h3></td>
    			</tr>
    			<tr class="row1"> 
      				<td width="32%"><div style="text-align: right; font-weight: bold;">
   					  Tên:</div>      				</td>
      				<td width="68%"> 
       				  <input name="ts1" size="32" style="width:200px; margin-left:10px" maxlength="255" type="text" value="<?php echo $dulieu['NV_TEN'] ?>">      				</td>
    			</tr>
    			<tr class="row0"> 
      				<td width="32%"><div style="text-align: right; font-weight: bold;">
					Tên đăng nhập:</div>      				</td>
      				<td width="68%"><input style="width:200px; margin-left:10px" name="ts2" size="32" maxlength="255" type="text" value="<?php echo $dulieu['NV_MA'] ?>"/></td>
    			</tr>
    			<tr class="row1"> 
      				<td width="32%"><div style="text-align: right; font-weight: bold;">Địa chỉ:</div>      				</td>
      				<td width="68%"><input style="width:200px; margin-left:10px" name="ts3" size="32" maxlength="255" type="text" value="<?php echo $dulieu['NV_DIACHI'] ?>" /></td>
    			</tr>
				<tr class="row0"> 
      				<td width="32%"><div style="text-align: right; font-weight: bold;">
					Số điện thoại:</div>      				</td>
      				<td width="68%"><input style="width:200px; margin-left:10px" name="ts4" size="32" maxlength="255" type="text" value="<?php echo $dulieu['NV_SDT'] ?>" /></td>
    			</tr>
                				<tr class="row1"> 
      				<td width="32%"><div style="text-align: right; font-weight: bold;">
					Email:</div>      				</td>
      				<td width="68%"><input style="width:200px; margin-left:10px" name="ts10" size="32" maxlength="255" type="text" value="<?php echo $dulieu['NV_EMAIL'] ?>" /></td>
    			</tr>
				 <tr class="row0"> 
				   <td width="32%"><div style="text-align: right; font-weight: bold;">Mật khẩu mới:</div>      				</td>
				   <td width="68%"><input style="width:200px; margin-left:10px" name="ts7" size="32" maxlength="255" type="text" /></td>
				   </tr>
				<tr class="row1"> 
      				<td width="32%"><div style="text-align: right; font-weight: bold;">Xác nhận mật khẩu:</div>      				</td>
      				<td width="68%"><input style="width:200px; margin-left:10px" name="ts8" size="32" maxlength="255" type="text"> </td>
    			</tr>
				<tr class="row0"> 
      				<td width="32%"><div style="text-align: right; font-weight: bold;">Khóa nhân viên:</div>      				</td>
      				<td width="68%">
					 <input type="radio" name="ts9" value="1" <?php if($dulieu['NV_HTRANG']==1) echo "checked='checked'" ; ?>/> Có
                      <input type="radio" name="ts9" value="0" <?php if($dulieu['NV_HTRANG']==0) echo "checked='checked'" ; ?> /> Không 
					</td>
    			</tr>
				<tr class="row1"> 
      				<td width="32%"><div style="text-align: right; font-weight: bold;">Ngày đăng ký:</div>      				</td>
      				<td width="68%">&nbsp;&nbsp;
<?php 
    echo substr($dulieu['NV_NGAYDK'],8,2)."-".substr($dulieu['NV_NGAYDK'],5,2)."-".substr($dulieu['NV_NGAYDK'],0,4).' &nbsp;'; 
	echo substr($dulieu['NV_NGAYDK'],11,2);
	echo substr($dulieu['NV_NGAYDK'],13,3);
?>
					</td>
    			</tr>
    			  <tr class="row0"> 
    					    
		      		<td valign="top" width="32%">&nbsp;</td>
		      		<td width="68%">&nbsp;</td>		    
	    			</tr>
	  			  </tbody></table>
 		</td>
 		<td width="527">&nbsp;</td>
  	</tr>
</tbody></table>

</div>
</div>
</form>
<?php
}// ket thuc update

if($_GET['action']=='update_tt' && $_GET['option']=='com_user')
{
         $query1 = "select * from `".$shop."`.`nhanvien` WHERE `NV_MA` = '".$_GET['id']."'";
		 $kq1 = mysql_query($query1);
		 $dongDL1 = mysql_fetch_array($kq1);
   if($dongDL1['NV_HTRANG']==0){
		$sql = "UPDATE `".$shop."`.`nhanvien` SET  `NV_HTRANG` = 1  WHERE `NV_MA` = '".$_GET['id']."'";
		$update = mysql_query($sql) or die($sql);
        echo"<script language=javascript>window.location='index.php?option=com_user&action=view';</script>";
   }
    if($dongDL1['NV_HTRANG']==1){
        $sql = "UPDATE `".$shop."`.`nhanvien` SET  `NV_HTRANG` = 0  WHERE `NV_MA` = '".$_GET['id']."'";
		$update = mysql_query($sql) or die($sql);
        echo"<script language=javascript>window.location='index.php?option=com_user&action=view';</script>";
   }
 }
?>


</div>
